package jacker.bit.price;

import android.app.IntentService;
import android.content.Context;
import android.content.Intent;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;

public class ServiceCheckPrice extends IntentService {

	public ServiceCheckPrice() {
		super("ServiceCheckMail");
	}

	@Override
	protected void onHandleIntent(Intent arg0) {
		PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
        WakeLock wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ServiceCheckPrice");
        wakeLock.acquire();
        
        try{
        	PriceChecker.checkPrices(getApplicationContext());
        }finally{
        	wakeLock.release();
        }
		
        int freq = PrefStore.getFetchFreq(this);
        
        if(freq == -1){ //fetch every 30 seconds
        	
        	try {
				Thread.sleep(29*1000);
				wakeLock.acquire();
				PriceChecker.checkPrices(getApplicationContext());
			} catch (InterruptedException e) {
				UtilLog.e(e);
			}finally{
	        	wakeLock.release();
	        }
        	
        }
		
	}

}
